/** 计算金额 */ import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
import { useSelector } from "react-redux";
import filters from "../utils/filters";
import Big from "big.js";
import { CHAINMAPTOSERVE } from "../config";
import { useMemo } from "react";
var useCalculateAmount = function() {
    var _ref = useSelector(function(state) {
        return {
            coininfo: state.app.tokenCoins,
            nftCoins: state.app.nftCoins
        };
    }) || {}, coininfo = _ref.coininfo, nftCoins = _ref.nftCoins;
    var coinMap = useMemo(function() {
        return _to_consumable_array(coininfo).concat(_to_consumable_array(nftCoins)).reduce(function(pre, next) {
            var _next_contractAddress;
            pre["".concat(next.chainType).concat((_next_contractAddress = next.contractAddress) === null || _next_contractAddress === void 0 ? void 0 : _next_contractAddress.toLocaleLowerCase())] = next;
            return pre;
        }, {});
    }, [
        coininfo.length,
        nftCoins.length
    ]);
    /** 计算美元的价格 wei: 计算为位  ether: 计算为美元（usd） */ var calculate = function(tokens) {
        var unit = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : "ether";
        return tokens.map(function(token) {
            var coin = coinMap["".concat(token.chainType).concat(token.contract.toLocaleLowerCase())];
            return new Big((coin === null || coin === void 0 ? void 0 : coin.price2USD) || 0).mul(token.balance).div(unit === "wei" ? 1 : "1e".concat((coin === null || coin === void 0 ? void 0 : coin.digits) || 0)).toString();
        });
    };
    var getMaxBalanceChain = function(tokens) {
        var totalSumByChainType = tokens.reduce(function(acc, param) {
            var chainType = param.chainType, convertActualAmount = param.convertActualAmount;
            acc[chainType] = Number(new Big(acc[chainType] || 0).plus(convertActualAmount).toString());
            return acc;
        }, {});
        // 找到总和最高的chainType
        var _Object_entries_reduce = _sliced_to_array(Object.entries(totalSumByChainType).reduce(function(acc, param) {
            var _param = _sliced_to_array(param, 2), chainType = _param[0], totalSum = _param[1];
            return totalSum > acc[1] ? [
                chainType,
                totalSum
            ] : acc;
        }, [
            null,
            -Infinity
        ]), 2), maxChainType = _Object_entries_reduce[0], tmp = _Object_entries_reduce[1], maxTotalSum = tmp === void 0 ? 0 : tmp;
        return {
            maxChainType: Number(maxTotalSum) > 0 ? CHAINMAPTOSERVE[maxChainType] : "ETHEREUM",
            maxTotalSum: maxTotalSum
        };
    };
    // const totalSumByChainType = data.reduce((acc, { chainType, price2USD }) => {
    //   acc[chainType] = (acc[chainType] || 0) + price2USD;
    //   return acc;
    // }, {});
    // // 找到总和最高的chainType
    // const [maxChainType, maxTotalSum] = Object.entries(totalSumByChainType).reduce(
    //   (acc, [chainType, totalSum]) => (totalSum > acc[1] ? [chainType, totalSum] : acc),
    //   [null, -Infinity]
    // );
    var toFormatUSD = function(val, digits) {
        var finalDigits = digits;
        if (typeof digits === "object") {
            var _coinMap_;
            finalDigits = ((_coinMap_ = coinMap["".concat(digits.chainType).concat(digits.contract.toLocaleLowerCase())]) === null || _coinMap_ === void 0 ? void 0 : _coinMap_.digits) || 0;
        }
        try {
            return filters.numFormat(new Big(val).div("1e".concat(finalDigits)).round(3).toString());
        } catch (e) {
            return "0";
        }
    };
    var toFormatEther = function(wei, digits) {
        var finalDigits = digits;
        if (typeof digits === "object") {
            var _coinMap_;
            finalDigits = ((_coinMap_ = coinMap["".concat(digits.chainType).concat(digits.contract.toLocaleLowerCase())]) === null || _coinMap_ === void 0 ? void 0 : _coinMap_.digits) || 0;
        }
        try {
            return filters.numFormat(new Big(wei).div("1e".concat(finalDigits)).round(6).toString());
        } catch (e) {
            return "0";
        }
    };
    /** 拼接nftImg */ var getNftImgSrc = function(tokenId, contract) {
        var _coin_baseURI;
        if (!tokenId) return "";
        var coin = coinMap["".concat(contract.chainType).concat(contract.contract.toLocaleLowerCase())];
        var _coin_baseURI_replace;
        var baseUrl = (_coin_baseURI_replace = (_coin_baseURI = coin.baseURI) === null || _coin_baseURI === void 0 ? void 0 : _coin_baseURI.replace("/json/", "/")) !== null && _coin_baseURI_replace !== void 0 ? _coin_baseURI_replace : "";
        return "".concat(baseUrl, "/").concat(tokenId, ".png");
    };
    return {
        calculate: calculate,
        coinMap: coinMap,
        toFormatEther: toFormatEther,
        toFormatUSD: toFormatUSD,
        getNftImgSrc: getNftImgSrc,
        getMaxBalanceChain: getMaxBalanceChain
    };
};
export default useCalculateAmount;

 //# sourceMappingURL=useCalculateAmount.js.map